package com.hfut.eeg.service;

import com.hfut.eeg.common.util.BlobListTrans;
import com.hfut.eeg.common.util.EEGUtil;
import com.hfut.eeg.dao.BrainChannelMapper;
import com.hfut.eeg.dto.BrainEnergy;
import com.hfut.eeg.dto.EEGTrans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.service
 * @Description: TODO
 * @date 2019-12-11 10:17
 */
@Service
@Scope("singleton")
public class BraninChannelService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BraninChannelService.class);
    @Autowired
    private BrainChannelMapper brainChannelMapper;

    @Value("${eeg.brainChannelFS}")
    private int brainChannelFS;

    public int insertBrainChannel(EEGTrans trans){
        return brainChannelMapper.insertBrainChannel(trans);
    }

    public int updateBrainChannel(EEGTrans trans){
        return brainChannelMapper.updateBrainChannel(trans);
    }

    public EEGTrans getBrainChannel(String eegId){
        return brainChannelMapper.getBrainChannel(eegId);
    }

    /**
    * @Description: 利用数组插入脑部通道数据
    * @param lists FFT数据数组 eegId 病人ID
    * @author 梁琦
    * @date 2019-12-30 20:16
    * @throws
    */
    public int insertBrainChannelByArr(ArrayList<ArrayList>lists,String eegId){
        ArrayList<ArrayList> tmp = getBrainChannelByArr(eegId,0,-1);
        EEGTrans trans = new EEGTrans();
        //表述数据库还没有这个数据，需要插入
        if(tmp == null){
            trans = (EEGTrans) BlobListTrans.transArrToEEG(lists,trans);
            trans.setEegId(eegId);
            return insertBrainChannel(trans);
        }
        //更新数据库
        if(lists.size()!=tmp.size()){
            LOGGER.error("插入数据与数据库大小不匹配：");
            return -1;
        }
        for (int i=0;i<tmp.size();i++){
            tmp.get(i).addAll(lists.get(i));
        }
        trans = (EEGTrans) BlobListTrans.transArrToEEG(tmp,trans);
        trans.setEegId(eegId);
        return updateBrainChannel(trans);
    }

    /**
    * @Description:
    * @param null
    * @return
    * @author 梁琦
    * @date 2019-12-30 20:23
    * @throws
    */
    public ArrayList getBrainChannelBySeg(String eegId,int start,int num){
        EEGTrans eegTrans = brainChannelMapper.getBrainChannel(eegId);
        ArrayList<ArrayList>eegs =  EEGUtil.getSimpleEEGBySeg(eegTrans,start,num);
        if(eegs == null || eegs.size()==0){
            return null;
        }
        //脑力通道图计算
        return EEGUtil.listBrainChannel(eegs);
    }

    public ArrayList<ArrayList> getBrainChannelByArr(String eegId,int start,int num){
        EEGTrans eegTrans = brainChannelMapper.getBrainChannel(eegId);
        ArrayList<ArrayList>eegs =  EEGUtil.getSimpleEEGBySeg(eegTrans,start,num);
        if(eegs == null || eegs.size()==0){
            return null;
        }
        return eegs;
    }
}
